﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace Web
{
    public partial class editInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["username"] = "tom";

                if (null != Session["username"])
                {
                    Label_State.Text = "已登录";
                    Label_User.Text = Session["username"].ToString();
                    Label_UserName.Text = Session["username"].ToString();


                    DAL.UserInfoDAL userInfoDAL = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
                    Model.UserInfo userInfoModel = userInfoDAL.userNameUserInfo(Session["username"].ToString());
                    TextBox_PSD.Text = userInfoModel.getUI_password();
                    TextBox_NickName.Text = userInfoModel.getUI_nickName();
                    DropDownList_Gender.SelectedIndex = userInfoModel.getUI_gender() - 1;

                    TextBox_QQ.Text = userInfoModel.getUI_qqNumber();
                    TextBox_Phone.Text = userInfoModel.getUI_phoneNumber();
                    TextBox_Country.Text = userInfoModel.getUI_country();
                    TextBox_Province.Text = userInfoModel.getUI_province();
                    TextBox_City.Text = userInfoModel.getUI_city();
                    TextBox_Remark.Text = userInfoModel.getUI_remark();


                    //if (!IsPostBack)
                    //{
                        int i;
                        ArrayList month = new ArrayList();
                        for (i = 1; i <= 12; i++)
                            month.Add(i);
                        this.DropDownList2.DataSource = month;
                        this.DropDownList2.DataBind();    //以上初始化月    
                        int j;
                        ArrayList year = new ArrayList();
                        for (j = 1900; j <= 2050; j++)
                            year.Add(j);
                        this.DropDownList1.DataSource = year;
                        this.DropDownList1.DataBind();    //以上初始化年    
                        this.DropDownList1.AutoPostBack = true;
                        this.DropDownList2.AutoPostBack = true;
                        daynum("30");
                    //}

                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            string userKey = TextBox_Search.Text;
            string url = "SearchResult.aspx?key=" + TextBox_Search.Text;
            Response.Redirect(url);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Index.aspx");
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            string userName =  Session["username"].ToString();
            string userPsd = TextBox_PSD.Text;
            string nickname = TextBox_NickName.Text;
            int gender = DropDownList_Gender.SelectedIndex ;
            string year = DropDownList1.Text;
            string month = DropDownList2.Text;
            string day = DropDownList3.Text;
            DateTime bithday = Convert.ToDateTime(year + "-" + month + "-" + day);
            string qqnum = TextBox_QQ.Text;
            string phoneNum = TextBox_Phone.Text;
            string country = TextBox_Country.Text;
            string province = TextBox_Province.Text;
            string city = TextBox_City.Text;
            string remark = TextBox_Remark.Text;

            DAL.UserInfoDAL userInfoDAL = (DAL.UserInfoDAL)DALFAC.DataAccess.CreateUserInfo();
            Model.UserInfo userInfoModel = userInfoDAL.userNameUserInfo(userName);
            userInfoModel.setUI_password(userPsd);
            userInfoModel.setUI_nickName(nickname);
            userInfoModel.setUI_gender(gender);
            userInfoModel.setUI_birthday(bithday);
            userInfoModel.setUI_qqNumber(qqnum);
            userInfoModel.setUI_phoneNumber(phoneNum);
            userInfoModel.setUI_country(country);
            userInfoModel.setUI_province(province);
            userInfoModel.setUI_city(city);
            userInfoModel.setUI_remark(remark);

            BLL.UserBLL userBLL = (BLL.UserBLL)BLLFAC.DataAccess.CreateUser();
            if (userBLL.modifyUser(userInfoModel))
            {
                Response.Redirect("usermain.aspx");
            }else {
                Response.Redirect("myInfo.aspx");
            }
        }

        protected void Quit_Click(object sender, EventArgs e)
        {
            Response.Redirect("myInfo.aspx");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            day(this.DropDownList2.SelectedValue, Convert.ToInt16(this.DropDownList1.SelectedValue));
        }

        private void day(string d, int year)
        {
            switch (d)
            {
                case "1":
                case "3":
                case "5":
                case "7":
                case "8":
                case "10":
                case "12":
                    daynum("31");
                    break;
                case "2":
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                        daynum("29");
                    else
                        daynum("28");
                    break;
                default:
                    daynum("30");
                    break;
            }
        }

        private void daynum(string num)
        {
            int i;
            ArrayList day = new ArrayList();
            for (i = 1; i <= Convert.ToInt16(num); i++)
                day.Add(i);
            this.DropDownList3.DataSource = day;
            this.DropDownList3.DataBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            day(this.DropDownList2.SelectedValue, Convert.ToInt16(this.DropDownList1.SelectedValue));
        }
    }
}